package com.rover.system.controller;

import com.grid.DataGrid;
import com.grid.Page;

import com.rover.system.entity.User;
import com.rover.system.service.UserLogService;
import com.rover.system.service.UserService;
import com.utils.JsonUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 公开访问
 */
@Controller
@RequestMapping("/userLogController")
public class UserLogController {

    @Autowired
    private UserLogService userLogService;

    @Autowired
    private UserService userService;

    @RequestMapping("/query")
    @ResponseBody
    public Map<String, Object> query(HttpServletRequest request, Page page, HttpServletResponse response) throws Exception {

        String loginName = (String) request.getParameter("loginName");
        String hql = "";
        if (StringUtils.isNotBlank(loginName)) {
            hql = "where loginName like '%" + loginName + "%'";
        }
        hql = "from User " + hql;
        List<User> users = userService.<User>findByHql(hql);
        List<Integer> ids = new ArrayList<Integer>();
        for (User user : users) {
            ids.add(user.getId());
        }
        Map<String, Object> params = new HashMap<String, Object>();

        DataGrid dataGrid = userLogService.query(page, params, users, ids, hql);
        return JsonUtil.JSONString(dataGrid.getResult(),
                dataGrid.getTotalCount());

    }

}
